package main;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.Vector;

import javax.swing.AbstractButton;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.JToggleButton;
import javax.swing.ListSelectionModel;
import javax.swing.UIManager;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;

import util.ApkTableModel;
import util.gestorSQL;
import util.progressDialog;

public class MainGui {

	protected static final String BACKUPS_FOLDER = "backups/";

	private JFrame frame;

	Locale idioma;
	ResourceBundle resourceBundle;
	private JTextField netAdbIp;
	private JCheckBox useNetAdbCheckBox;
	private JPanel cards_panel_main;

	private String START_PANEL = "start_panel";
	private String CONNECTION_PANEL = "connection_panel";
	private String PANEL_TABLE_APK = "table_apk_panel";
	private String PANEL_RESTORE = "panel_restore"; // panel_restore
	private String PANEL_RESTORE_TABLE = "panel_restore_table";
	// Device apk list
	private JTable tablaApks = new JTable();
	private ApkTableModel mdl;
	// Device list
	DefaultListModel listModel;
	private JList devicesList;

	Vector<AppApk> listaApksOut;
	String deviceSerial = "";
	// Backup sys data
	private JPanel panel_sys_data;
	private JCheckBox mmsCheckBox;
	private JCheckBox wifiCheckBox;
	private JToggleButton sysAppsToggleButton;
	// Backup list
	DefaultListModel backupListModel;
	private JList backupsList;
	// Restore
	//String backupRootFolder = "";

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MainGui window = new MainGui();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public MainGui() {
		initialConfigurations();
		initialize();
	}

	private void initialConfigurations() {
		idioma = iniciarIdioma();
		resourceBundle = ResourceBundle.getBundle("resources.LabelsBundle",
				idioma);
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setIconImage(Toolkit.getDefaultToolkit().getImage(
				MainGui.class.getResource("/resources/Android-green-64.png")));
		frame.setBounds(100, 100, 450, 477);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.addWindowListener(new WindowListener() {

			@Override
			public void windowClosing(WindowEvent arg0) {
				int n = JOptionPane.showConfirmDialog(frame,
						"Are you sure? I think no...", "Exit",
						JOptionPane.YES_NO_OPTION);
				System.out.println("option " + n);
				AdbCommands adbCmd = null;
				try {
					adbCmd = new AdbCommands(null);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				if (adbCmd != null) {
					adbCmd.disconnectAdb(true);
				}
			}

			@Override
			public void windowClosed(WindowEvent arg0) {
				// TODO Auto-generated method stub

			}

			@Override
			public void windowActivated(WindowEvent arg0) {
				// TODO Auto-generated method stub

			}

			@Override
			public void windowDeactivated(WindowEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void windowDeiconified(WindowEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void windowIconified(WindowEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void windowOpened(WindowEvent e) {
				// TODO Auto-generated method stub

			}
		});
		frame.setTitle(resourceBundle.getString("TITLE"));

		JPanel panelWithAll = new JPanel();
		frame.getContentPane().add(panelWithAll, BorderLayout.CENTER);
		panelWithAll.setLayout(new BorderLayout(0, 0));

		JMenuBar menuBar = new JMenuBar();
		panelWithAll.add(menuBar, BorderLayout.NORTH);

		JMenu mnNewMenu = new JMenu(resourceBundle.getString("FILE"));
		menuBar.add(mnNewMenu);

		cards_panel_main = new JPanel();
		panelWithAll.add(cards_panel_main, BorderLayout.CENTER);
		cards_panel_main.setLayout(new CardLayout(0, 0));

		JPanel panel_start = new JPanel();
		cards_panel_main.add(panel_start, START_PANEL);
		panel_start.setLayout(new BorderLayout(0, 0));

		JButton backupOptionButton = new JButton(
				resourceBundle.getString("BACKUP"));
		panel_start.add(backupOptionButton, BorderLayout.SOUTH);

		backupOptionButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				// TODO listar devices
				AdbCommands adbCmd = null;
				try {
					adbCmd = new AdbCommands(null);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				if (adbCmd.startAdbServer()) {
					Vector<String> out = new Vector<String>();
					adbCmd.listDevices(out);
					listModel.removeAllElements();
					if (out.size() > 1) {
						for (int i = 1; i < out.size(); i++) {
							listModel.add(i - 1, out.get(i));
						}
						devicesList
								.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
						devicesList.revalidate();
						devicesList.setSelectedIndex(0);
					} else {
						useNetAdbCheckBox.setSelected(true);
					}
					CardLayout cl = (CardLayout) (cards_panel_main.getLayout());
					cl.show(cards_panel_main, CONNECTION_PANEL);
				}
			}
		});

		JPanel panel_connection = new JPanel();
		cards_panel_main.add(panel_connection, CONNECTION_PANEL);
		panel_connection.setLayout(new BorderLayout(0, 0));

		JPanel panel_net_connection = new JPanel();
		panel_connection.add(panel_net_connection, BorderLayout.NORTH);

		useNetAdbCheckBox = new JCheckBox(
				resourceBundle.getString("USE_NET_ADB"));
		panel_net_connection.add(useNetAdbCheckBox);

		JLabel lblNewLabel = new JLabel(resourceBundle.getString("IP"));
		panel_net_connection.add(lblNewLabel);

		netAdbIp = new JTextField(resourceBundle.getString("DEFAULT_IP"));
		panel_net_connection.add(netAdbIp);
		netAdbIp.setColumns(10);

		JButton connectNetAdbButton = new JButton(
				resourceBundle.getString("CONNECT"));
		panel_net_connection.add(connectNetAdbButton);

		connectNetAdbButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				AdbCommands adbCmd = null;
				try {
					adbCmd = new AdbCommands(null);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				if (adbCmd.connectAdb(useNetAdbCheckBox.isSelected(),
						netAdbIp.getText())) {
					System.out.println("Connected");
					Vector<String> out = new Vector<String>();
					adbCmd.listDevices(out);
					listModel.removeAllElements();
					if (out.size() > 1) {
						for (int i = 1; i < out.size(); i++) {
							listModel.add(i - 1, out.get(i));
						}
						devicesList
								.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
						devicesList.revalidate();
						devicesList.setSelectedIndex(0);
					} else {
						useNetAdbCheckBox.setSelected(true);
					}
				}
			}
		});

		JPanel panel_usb_connection = new JPanel();
		panel_connection.add(panel_usb_connection, BorderLayout.CENTER);
		panel_usb_connection.setLayout(new BorderLayout(0, 0));

		JLabel lblNewLabel_1 = new JLabel(resourceBundle.getString("DEVICES"));
		panel_usb_connection.add(lblNewLabel_1, BorderLayout.NORTH);

		listModel = new DefaultListModel();
		devicesList = new JList(listModel);
		panel_usb_connection.add(devicesList, BorderLayout.CENTER);

		JPanel panel_next_connection = new JPanel();
		panel_connection.add(panel_next_connection, BorderLayout.SOUTH);

		JButton listApksButton = new JButton(
				resourceBundle.getString("LIST_APKS"));
		panel_next_connection.add(listApksButton);

		JButton restoreOptionButton = new JButton(
				resourceBundle.getString("RESTORE"));
		panel_next_connection.add(restoreOptionButton);

		restoreOptionButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				CardLayout cl = (CardLayout) (cards_panel_main.getLayout());
				// if (adbCmd.connectAdb(true,
				// useNetAdbCheckBox.isSelected(),netAdbIp.getText())) {
				if (devicesList.getSelectedValue() != null) {
					deviceSerial = devicesList.getSelectedValue().toString();
					Vector<String> folders = new MainFunctions()
							.listFolders(BACKUPS_FOLDER);
					backupListModel.removeAllElements();
					if (folders.size() > 1) {
						for (int i = 0; i < folders.size(); i++) {
							backupListModel.add(i, folders.get(i));
						}
						backupsList
								.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
						backupsList.revalidate();
						backupsList.setSelectedIndex(0);
						cl.show(cards_panel_main, PANEL_RESTORE);
						cards_panel_main.updateUI();
					}
				}
			}
		});

		listApksButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				// CardLayout cl = (CardLayout) (cards_panel_main.getLayout());
				// if (adbCmd.connectAdb(true,
				// useNetAdbCheckBox.isSelected(),netAdbIp.getText())) {
				if (devicesList.getSelectedValue() != null) {
					deviceSerial = devicesList.getSelectedValue().toString();
					listApks();
				}
				// }
			}
		});

		// Tabla de apks
		JPanel panel_table_apk = new JPanel();
		panel_table_apk.setLayout(new BorderLayout(0, 0));
		cards_panel_main.add(panel_table_apk, PANEL_TABLE_APK);

		JPanel panel_next_table = new JPanel();
		panel_table_apk.add(panel_next_table, BorderLayout.SOUTH);
		panel_next_table.setLayout(new BorderLayout(0, 0));

		JButton backupButton = new JButton(resourceBundle.getString("BACKUP"));
		panel_next_table.add(backupButton, BorderLayout.EAST);

		JPanel panel = new JPanel();
		panel_table_apk.add(panel, BorderLayout.CENTER);
		panel.setLayout(new BorderLayout(0, 0));

		JPanel panel_1 = new JPanel();
		panel.add(panel_1, BorderLayout.NORTH);
		panel_1.setLayout(new BorderLayout(0, 0));

		sysAppsToggleButton = new JToggleButton(
				resourceBundle.getString("SYS_APPS"), false);
		panel_1.add(sysAppsToggleButton, BorderLayout.NORTH);

		panel_sys_data = new JPanel();
		panel_sys_data.setVisible(false);
		panel_1.add(panel_sys_data);
		panel_sys_data.setLayout(new GridLayout(2, 0, 0, 0));

		mmsCheckBox = new JCheckBox(resourceBundle.getString("BACKUP_MMS_"));
		panel_sys_data.add(mmsCheckBox);

		wifiCheckBox = new JCheckBox(resourceBundle.getString("BACKUP_WIFI_"));
		panel_sys_data.add(wifiCheckBox);

		sysAppsToggleButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				AbstractButton abstractButton = (AbstractButton) arg0
						.getSource();
				boolean selected = abstractButton.getModel().isSelected();
				loadApks(listaApksOut, selected);
				panel_sys_data.setVisible(selected);
				if (!selected) {
					mmsCheckBox.setSelected(selected);
					wifiCheckBox.setSelected(selected);
				}
				frame.repaint();
			}
		});

		JScrollPane mScrollPane = new JScrollPane(tablaApks);
		panel.add(mScrollPane);
		tablaApks.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		configurarTabla();
		// tablaApks.setModel(mdl);

		JPanel panel_restore = new JPanel();
		cards_panel_main.add(panel_restore, PANEL_RESTORE);
		panel_restore.setLayout(new BorderLayout(0, 0));

		JLabel lblNewLabel_2 = new JLabel(
				resourceBundle.getString("RESTORE_FOLDERS"));
		panel_restore.add(lblNewLabel_2, BorderLayout.NORTH);

		backupListModel = new DefaultListModel();
		backupsList = new JList(backupListModel);
		panel_restore.add(backupsList, BorderLayout.CENTER);

		JPanel panel_2 = new JPanel();
		panel_restore.add(panel_2, BorderLayout.SOUTH);

		JButton selectBackupButton = new JButton(
				resourceBundle.getString("SELECT_BACKUP_FOLDER"));
		panel_2.add(selectBackupButton);
		selectBackupButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if (backupsList.getSelectedValue() != null) {
					String backupRootFolder = backupsList.getSelectedValue()
							.toString();
					backupsList.getSelectedIndex();
					listaApksOut = new Vector<AppApk>();
					listaApksOut = new MainFunctions()
							.listFolderApks(BACKUPS_FOLDER + backupRootFolder);
					loadApks(listaApksOut, false);
					CardLayout cl = (CardLayout) (cards_panel_main.getLayout());
					cl.show(cards_panel_main, PANEL_RESTORE_TABLE);
					cards_panel_main.updateUI();
				}
			}
		});

		JPanel panel_table_restore = new JPanel();
		cards_panel_main.add(panel_table_restore, PANEL_RESTORE_TABLE);
		panel_table_restore.setLayout(new BorderLayout(0, 0));

		JScrollPane scrollPane = new JScrollPane(tablaApks);
		panel_table_restore.add(scrollPane);

		tablaApks.getModel().addTableModelListener(new TableModelListener() {

			@Override
			public void tableChanged(TableModelEvent e) {
				// TODO Auto-generated method stub
				int row = e.getFirstRow();
				int column = e.getColumn();
				ApkTableModel model = (ApkTableModel) e.getSource();
				applyChangesTable(column, row, model);
			}
		});
		backupButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Calendar backupCal = Calendar.getInstance();
				String destDirectory = BACKUPS_FOLDER
						+ backupCal.get(Calendar.YEAR) + "_"
						+ (backupCal.get(Calendar.MONTH) + 1) + "_"
						+ backupCal.get(Calendar.DAY_OF_MONTH) + "_"
						+ backupCal.get(Calendar.HOUR_OF_DAY) + "_"
						+ backupCal.get(Calendar.MINUTE);
				backupApksAndData(listaApksOut, destDirectory);
			}
		});

		JPanel panel_Buttons = new JPanel();
		panelWithAll.add(panel_Buttons, BorderLayout.SOUTH);
		panel_Buttons.setLayout(new BorderLayout(0, 0));

		JButton cancelButton = new JButton(resourceBundle.getString("CANCEL"));
		panel_Buttons.add(cancelButton, BorderLayout.WEST);

		JButton btnTest = new JButton("test");
		btnTest.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				Thread t = new Thread(new Runnable() {

					@Override
					public void run() {
						// TODO Auto-generated method stub

						progressDialog pd = new progressDialog("test", 100);
						pd.start();
						for (int i = 0; i < 100; i++) {
							pd.setProgressDialoj(i);
							try {
								Thread.sleep(100);
							} catch (InterruptedException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}
					}
				});
				t.start();
			}
		});
		panel_Buttons.add(btnTest, BorderLayout.EAST);

		cancelButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				CardLayout cl = (CardLayout) (cards_panel_main.getLayout());
				// TODO restart adb
				cl.show(cards_panel_main, START_PANEL);
				AdbCommands adbCmd = null;
				try {
					adbCmd = new AdbCommands(null);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				adbCmd.disconnectAdb(true);
			}
		});
	}

	private void configurarTabla() {
		Object[] columnas = new Object[6];

		columnas[0] = resourceBundle.getString("SELECTED");
		columnas[1] = resourceBundle.getString("PACKAGE");
		columnas[2] = resourceBundle.getString("APK");
		columnas[3] = resourceBundle.getString("PATH");
		columnas[4] = resourceBundle.getString("SYSTEM_APP_");
		columnas[5] = resourceBundle.getString("BACKUP_DATA_");

		mdl = new ApkTableModel();
		mdl.setColumnIdentifiers(columnas);
		tablaApks.setModel(mdl);
		/**
		 * test
		 */
		// Select app
		TableColumn tc = tablaApks.getColumnModel().getColumn(0);
		tc.setCellEditor(tablaApks.getDefaultEditor(Boolean.class));
		tc.setCellRenderer(tablaApks.getDefaultRenderer(Boolean.class));
		tc.setHeaderRenderer(new util.CheckBoxHeader(
				new MyCheckAllItemListener(0), resourceBundle
						.getString("SELECTED")));
		// Select data
		tc = tablaApks.getColumnModel().getColumn(5);
		tc.setCellEditor(tablaApks.getDefaultEditor(Boolean.class));
		tc.setCellRenderer(tablaApks.getDefaultRenderer(Boolean.class));
		tc.setHeaderRenderer(new util.CheckBoxHeader(
				new MyCheckAllItemListener(5), resourceBundle
						.getString("BACKUP_DATA_")));
	}

	private int getPackagePos(Vector<AppApk> listaApksOut, String packageName,
			String path) {
		AppApk apkTemp = new AppApk(path, packageName, "");
		for (int index = 0; index < listaApksOut.size(); index++) {
			if (listaApksOut.get(index).equals(apkTemp)) {
				return index;
			}
		}
		return -1;
	}

	private void loadApks(Vector<AppApk> out, boolean includeSysApps) {
		Object[] valores = new Object[6];
		// System.out.println("filas " + tablaApks.getColumnCount());
		mdl.setRowCount(0);
		// System.out.println("count " + mdl.getRowCount());
		if (out != null) {
			for (int i = 0; i < out.size(); i++) {
				if (out.get(i).isSystenApp()) {
					if (includeSysApps) {
						valores[0] = out.get(i).isSelected();
						valores[1] = out.get(i).getPackageName();
						valores[2] = out.get(i).getName();
						valores[3] = out.get(i).getPathApk();
						valores[4] = out.get(i).isSystenApp();
						valores[5] = out.get(i).isData();
						mdl.addRow(valores);
					}
				} else {
					valores[0] = out.get(i).isSelected();
					valores[1] = out.get(i).getPackageName();
					valores[2] = out.get(i).getName();
					valores[3] = out.get(i).getPathApk();
					valores[4] = out.get(i).isSystenApp();
					valores[5] = out.get(i).isData();
					mdl.addRow(valores);
				}
			}
		}
		tablaApks.revalidate();
	}

	private void backupApksAndData(final Vector<AppApk> out,
			final String destDirectory) {
		// TODO test this...:S
		Thread t = new Thread(new Runnable() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				enableMainFrame(false);
				new MainFunctions().backupApkData(out, destDirectory,
						deviceSerial);
				loadApks(out, sysAppsToggleButton.isSelected());

				Vector<String> dataPath = new Vector<String>();
				if (addSysDataOptions(dataPath)) {
					new MainFunctions().backupSysData(dataPath, destDirectory,
							deviceSerial);
				}
				enableMainFrame(true);
			}
		});
		t.start();
		// new MainFunctions().backupApkData(out, destDirectory, deviceSerial);
		// loadApks(out, sysAppsToggleButton.isSelected());
	}

	private void listApks() {
		Thread t = new Thread(new Runnable() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				enableMainFrame(false);
				progressDialog mProgressDialog = new progressDialog(
						"Reading apps list");
				mProgressDialog.start();
				listaApksOut = new Vector<AppApk>();
				AdbCommands adbCmd = null;
				try {
					adbCmd = new AdbCommands(deviceSerial);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				if (adbCmd.listAllPackages(listaApksOut)) {
					if (listaApksOut.size() > 0) {
						loadApks(listaApksOut, false);
						CardLayout cl = (CardLayout) (cards_panel_main
								.getLayout());
						cl.show(cards_panel_main, PANEL_TABLE_APK);
						cards_panel_main.updateUI();
					}
				}
				mProgressDialog.closeProgressDialog();
				enableMainFrame(true);
			}
		});
		t.start();
	}

	private boolean addSysDataOptions(Vector<String> dataPath) {
		// /data/data/com.android.providers.telephony/databases/mmssms.db
		String mms = "/data/data/com.android.providers.telephony/databases/mmssms.db";
		String wifi = "/data/misc/wifi";
		if (mmsCheckBox.isSelected()) {
			dataPath.add(mms);
		}
		if (wifiCheckBox.isSelected()) {
			dataPath.add(wifi);
		}
		return (dataPath.size() > 0);
	}

	private void applyChangesTable(int column, int row, ApkTableModel model) {
		if ((row >= 0) && (column >= 0)) {
			Object dataChanged = model.getValueAt(row, column);
			// System.out.println("row " + row + " col " + column);
			// System.out.println(dataChanged.toString());
			if ((column == 0) || column == 5) {
				// System.out.println("cambio en tabla");
				Object data = model.getValueAt(row, 1);
				String packageName = data.toString();
				data = model.getValueAt(row, 3);
				String path = data.toString();
				// System.out.println(packageName);
				int apkPos = getPackagePos(listaApksOut, packageName, path);
				// System.out.println("posicion listaApksOut " + apkPos);
				if (apkPos >= 0) {
					AppApk updatedApk = listaApksOut.get(apkPos);
					if (column == 0) {
						updatedApk.setSelected((dataChanged.toString()
								.equalsIgnoreCase("true")));
					} else if (column == 5) {
						updatedApk.setData((dataChanged.toString()
								.equalsIgnoreCase("true")));
					}
					listaApksOut.set(apkPos, updatedApk);
				}
			}
		}
	}

	private static Locale iniciarIdioma() {
		// TODO leer un fichero para decidir el idioma �? y guardar una
		// variable
		// para cargar todo
		String idioma = null;
		gestorSQL gSQL = new gestorSQL();
		String rs = gSQL.getConfiguracion(gestorSQL.IDIOMA);
		if (rs != null) {
			idioma = rs;
		}
		// String idioma = LectorXML.leerXML("idioma.xml", "idioma", "locale");
		if (idioma == null) {
			idioma = Locale.getDefault().toString();
		}

		Locale seleccionado = new Locale(idioma);
		/*
		 * ResourceBundle resourceBundle = ResourceBundle.getBundle(
		 * "resources.LabelsBundle", Locale.ENGLISH);
		 */
		return seleccionado;
	}

	private void enableMainFrame(boolean enable) {
		frame.setEnabled(enable);
		frame.setAlwaysOnTop(true);
		frame.setAlwaysOnTop(false);
	}

	/**
	 * ItemListener for tables with checkall header
	 * 
	 */
	class MyCheckAllItemListener implements ItemListener {
		private int column = 0;

		public MyCheckAllItemListener(int mColumn) {
			column = mColumn;
		}

		public void itemStateChanged(ItemEvent e) {
			Object source = e.getSource();
			if (source instanceof AbstractButton == false)
				return;
			boolean checked = e.getStateChange() == ItemEvent.SELECTED;
			for (int x = 0, y = tablaApks.getRowCount(); x < y; x++) {
				tablaApks.setValueAt(new Boolean(checked), x, column);
			}
		}
	}
}
